using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace Rensselaer_Event_Finder
{
    public partial class AllEventsMDI : Form
    {

        #region Variables

        public List<DateTime> visibleDays;
        public int categoryID = -1; 

        #endregion

        #region Public Methods

        public AllEventsMDI()
        {
            InitializeComponent();
            visibleDays = new List<DateTime>();
            DateTime today = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            visibleDays.Add(today);
            visibleDays.Add(today.AddDays(1));
            visibleDays.Add(today.AddDays(2));
        }

        /// <summary>
        /// Load events in the visible days based on the category ID given
        /// </summary>
        /// <param name="categoryID">Id of the category to display (-1 = all categories)</param>
        public void load_events(int catID)
        {
            categoryID = catID;
            updateDaysShown();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Add Event to the All Events Page
        /// </summary>
        /// <param name="newEvent">Event to add</param>
        /// <param name="day">Day 1, 2, or 3 to specify which panel to add to</param>
        private void addEvent(Event newEvent, int day)
        {
            //controls to be added for each event
            Panel newPanel = new System.Windows.Forms.Panel();
            Label newTime = new System.Windows.Forms.Label();
            Label newCategory = new System.Windows.Forms.Label();
            Label newVenue = new System.Windows.Forms.Label();
            LinkLabel newEventName = new System.Windows.Forms.LinkLabel();

            // 
            // newPanel
            // 
            newPanel.Controls.Add(newEventName);
            newPanel.Controls.Add(newTime);
            newPanel.Controls.Add(newCategory);
            newPanel.Controls.Add(newVenue);
            newPanel.Location = new System.Drawing.Point(3, 3);
            newPanel.Name = newEvent.Name + "panel";
            newPanel.Size = new System.Drawing.Size(248, 105);
            newPanel.TabIndex = 0;
            newPanel.BorderStyle = BorderStyle.Fixed3D;
            newPanel.ForeColor = Color.White;
            // 
            // newTime
            // 
            newTime.AutoSize = true;
            newTime.Location = new System.Drawing.Point(7, 55);
            newTime.Name = newEvent.Name + "Time";
            newTime.Size = new System.Drawing.Size(69, 12);
            newTime.TabIndex = 7;
            newTime.Text = newEvent.StartTime.ToShortTimeString();
            newTime.BackColor = Color.Black;
            newTime.ForeColor = Color.White;
            newTime.Font = new Font("Microsoft Sans Serif", 12, FontStyle.Regular,
                                        GraphicsUnit.Point, ((byte)(0)));
            // 
            // newCategory
            // 
            newCategory.AutoSize = true;
            newCategory.Location = new System.Drawing.Point(85, 55);
            newCategory.Size = new System.Drawing.Size(149, 12);
            newCategory.Name = newEvent.Name + "Category";
            newCategory.TabIndex = 3;
            newCategory.Text = newEvent.Categories[0].Name;
            newCategory.Font = new Font("Microsoft Sans Serif", 12, FontStyle.Regular, 
                                        GraphicsUnit.Point, ((byte)(0)));
            newCategory.BackColor = Color.Black;
            newCategory.ForeColor = Color.White;
            // 
            // newVenue
            // 
            newVenue.AutoEllipsis = true;
            newVenue.Location = new System.Drawing.Point(7, 79);
            newVenue.Size = new System.Drawing.Size(222, 25);
            newVenue.Name = "label1";
            newVenue.TabIndex = 1;
            newVenue.Text = newEvent.Location.Name;
            newVenue.BackColor = Color.Black;
            newVenue.ForeColor = Color.White;
            newVenue.Font = new Font("Microsoft Sans Serif", 12, FontStyle.Regular,
                                    GraphicsUnit.Point, ((byte)(0)));
            // 
            // newEventName
            // 
            newEventName.AutoEllipsis = true;
            newEventName.DisabledLinkColor = System.Drawing.Color.White;
            newEventName.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            newEventName.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;
            newEventName.LinkColor = System.Drawing.Color.Firebrick;
            newEventName.Location = new System.Drawing.Point(6, 9);
            newEventName.Name = "linkLabel1";
            newEventName.Size = new System.Drawing.Size(233, 43);
            newEventName.TabIndex = 0;
            newEventName.TabStop = true;
            //add event name to text field, id to tag, and event handler for click event
            newEventName.Text = newEvent.Name.ToString();
            newEventName.ActiveLinkColor = System.Drawing.Color.White;
            newEventName.BackColor = Color.Black;
            newEventName.ForeColor = Color.White;
            newEventName.LinkColor = Color.White;
            newEventName.Tag = newEvent.Id;
            newEventName.Click += new EventHandler(EventName_Click);

            //add to correct panel
            switch (day)
            {
                case 1:
                    day1Panel.Controls.Add(newPanel);
                    break;
                case 2:
                    day2Panel.Controls.Add(newPanel);
                    break;
                case 3:
                    day3Panel.Controls.Add(newPanel);
                    break;
            }
        }

        /// <summary>
        /// Clears all event info from the panels
        /// </summary>
        private void clearAllEvents()
        {
            day1Panel.Controls.Clear();
            day2Panel.Controls.Clear();
            day3Panel.Controls.Clear();
        }

        /// <summary>
        /// Updates labels with changed visibleDays
        /// </summary>
        private void updateDaysShown()
        {
            clearAllEvents();

            day1Label.Text = visibleDays[0].ToShortDateString();
            day2Label.Text = visibleDays[1].ToShortDateString();
            day3Label.Text = visibleDays[2].ToShortDateString();

            IList<Event> day1Events = new List<Event>();
            IList<Event> day2Events = new List<Event>();
            IList<Event> day3Events = new List<Event>();

            //get each day's events in specified category
            if (categoryID != -1)
            {
                day1Events = Globals.getEventService().getEventsByCategoryInRange(categoryID,
                                                            visibleDays[0], visibleDays[0].AddDays(.9999));
                day2Events = Globals.getEventService().getEventsByCategoryInRange(categoryID,
                                                            visibleDays[1], visibleDays[1].AddDays(.9999));
                day3Events = Globals.getEventService().getEventsByCategoryInRange(categoryID,
                                                            visibleDays[2], visibleDays[2].AddDays(.9999));
            }
            else //get each day's events for all categories
            {
                //load each day with all categories
                day1Events = Globals.getEventService().getEventsInRange(visibleDays[0],
                                                         visibleDays[0].AddDays(.9999));
                day2Events = Globals.getEventService().getEventsInRange(visibleDays[1],
                                                         visibleDays[1].AddDays(.9999));
                day3Events = Globals.getEventService().getEventsInRange(visibleDays[2],
                                                         visibleDays[2].AddDays(.9999));
            }

            //if events exist, add each one
            if (day1Events.Count > 0)
            {
                foreach (Event newEvent in day1Events)
                {
                    addEvent(newEvent, 1);
                }
            }
            else  //show the no events label
            {
                //label to show if no events for this day
                Label noEventsLbl = new Label();
                noEventsLbl.Width = day3Panel.Width - 10;
                noEventsLbl.ForeColor = Color.White;
                noEventsLbl.TextAlign = ContentAlignment.BottomCenter;
                noEventsLbl.Text = "No Events Available";
                day1Panel.Controls.Add(noEventsLbl);
            }

            //repeat for day2
            if (day2Events.Count > 0)
            {
                foreach (Event newEvent in day2Events)
                {
                    addEvent(newEvent, 2);
                }
            }
            else
            {
                //label to show if no events for this day
                Label noEventsLbl = new Label();
                noEventsLbl.Width = day3Panel.Width - 10;
                noEventsLbl.ForeColor = Color.White;
                noEventsLbl.TextAlign = ContentAlignment.BottomCenter;
                noEventsLbl.Text = "No Events Available";
                day2Panel.Controls.Add(noEventsLbl);
            }


            //repeat for day3
            if (day3Events.Count > 0)
            {
                foreach (Event newEvent in day3Events)
                {
                    addEvent(newEvent, 3);
                }
            }
            else
            {
                //label to show if no events for this day
                Label noEventsLbl = new Label();
                noEventsLbl.Width = day3Panel.Width - 10;
                noEventsLbl.ForeColor = Color.White;
                noEventsLbl.TextAlign = ContentAlignment.BottomCenter;
                noEventsLbl.Text = "No Events Available";
                day3Panel.Controls.Add(noEventsLbl);
            }

        }

        #endregion

        #region Event Handlers

        private void homeBtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MainForm.SelectMDIForm(MainForm.homeFrm);
        }

        private void EventName_Click(object sender, EventArgs e)
        {
            //load event page from id corresponding to link's tag
            LinkLabel senderLink = (LinkLabel)sender;
            Event loadEvent = Globals.getEventService().getById((int)senderLink.Tag);
            MainForm.eventFrm.load_event(loadEvent);
            MainForm.SelectMDIForm(MainForm.eventFrm);
        }

        private void AllEventsMDI_Load(object sender, EventArgs e)
        {
            updateDaysShown();
        }

        private void prevDaysBtn_Click(object sender, EventArgs e)
        {
            //change visible days to previous 3
            visibleDays[0] = visibleDays[0].AddDays(-3);
            visibleDays[1] = visibleDays[1].AddDays(-3);
            visibleDays[2] = visibleDays[2].AddDays(-3);

            updateDaysShown();
        }

        private void nextDaysBtn_Click(object sender, EventArgs e)
        {
            //change visible days to next 3
            visibleDays[0] = visibleDays[0].AddDays(3);
            visibleDays[1] = visibleDays[1].AddDays(3);
            visibleDays[2] = visibleDays[2].AddDays(3);

            updateDaysShown();
        }

        #endregion

    }
}